package cn.lili.controller.user;

import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.member.entity.dos.UserAddress;
import cn.lili.modules.member.entity.dto.AddressSearchParams;
import cn.lili.modules.member.entity.enums.AddressTypeEnum;
import cn.lili.modules.member.service.UserAddressService;
import cn.lili.mybatis.util.SceneHelp;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 地址API
 *
 * @author Bulbasaur
 * @since 2020-02-25 14:10:16
 */
@RestController
@Api(tags = "地址API")
@RequestMapping("/user/address")
@RequiredArgsConstructor
@Validated
public class UserAddressController {

    private final UserAddressService userAddressService;

    @PreventDuplicateSubmissions
    @ApiOperation(value = "新增地址")
    @PostMapping
    public ResultMessage<UserAddress> addShippingAddress(@Valid @RequestBody UserAddress userAddress) {
        //非管理员，则将当前token中的用户信息写入地址
        if (!Objects.requireNonNull(UserContext.getCurrentUser()).getScene().equals(SceneEnums.MANAGER)) {
            //写入当前用户ID
            SceneHelp.objectHandler(userAddress);
        }
        //添加地址
        return ResultUtil.data(userAddressService.saveUserAddress(userAddress));
    }

    @ApiOperation(value = "修改地址")
    @PutMapping
    public ResultMessage<UserAddress> editShippingAddress(@Valid @RequestBody UserAddress userAddress) {
        //非管理员，则将当前token中的用户信息写入地址
        if (!Objects.requireNonNull(UserContext.getCurrentUser()).getScene().equals(SceneEnums.MANAGER)) {
            //写入当前用户ID
            SceneHelp.objectHandler(userAddress);
        }
        //修改地址
        return ResultUtil.data(userAddressService.updateUserAddress(userAddress));
    }

    @ApiOperation(value = "修改默认地址")
    @PutMapping("/default/{addressId}")
    public ResultMessage<UserAddress> updateDefault(@PathVariable String addressId) {
        userAddressService.updateDefaultAddress(addressId);
        //修改地址
        return ResultUtil.success();
    }

    @ApiOperation(value = "根据ID获取地址")
    @ApiImplicitParam(name = "id", value = "地址ID", dataType = "String", paramType = "path")
    @GetMapping(value = "/{id}")
    public ResultMessage<UserAddress> getShippingAddress(@PathVariable String id) {
        return ResultUtil.data(userAddressService.getUserAddress(id));
    }

    @ApiOperation(value = "获取当前默认地址")
    @GetMapping(value = "/default")
    public ResultMessage<UserAddress> getDefaultShippingAddress(String type) {

        return ResultUtil.data(userAddressService.getUserDefaultAddress(null));
    }


    @ApiOperation(value = "获取地址分页列表")
    @GetMapping
    public ResultMessage<Page<UserAddress>> page(PageVO page, AddressSearchParams addressSearchParams) {
        return ResultUtil.data(userAddressService.addressPage(page, addressSearchParams));
    }

    @ApiOperation(value = "获取地址分页列表")
    @GetMapping("/self-pickup")
    public ResultMessage<Page<UserAddress>> selfPickPage(PageVO page, AddressSearchParams addressSearchParams) {
        addressSearchParams.setType(AddressTypeEnum.SELF_PICKUP);
        return ResultUtil.data(userAddressService.addressPageNoAuth(page, addressSearchParams));
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "删除地址")
    @ApiImplicitParam(name = "id", value = "地址ID", dataType = "String", paramType = "path")
    @DeleteMapping(value = "/{id}")
    public ResultMessage<Object> delShippingAddressById(@PathVariable String id) {
        userAddressService.removeUserAddress(id);
        return ResultUtil.success();
    }


}
